package com.doone.telmarket.domain.factory;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doone.telmarket.domain.ServiceOrder;
import com.doone.telmarket.domain.ServicePage;
import com.doone.telmarket.domain.ServicePageContent;
import com.doone.telmarket.domain.SiteDirectory;
import com.doone.telmarket.domain.SiteResource;

/**
 * 
 * @author 吴同庆
 * @date 2009-9-17
 * @description 增值业务页面工厂
 * @package     com.doone.telmarket.domain.factory
 */
@Service
public class ServicePageFactory {

	@Autowired
	private SiteResourceFactory siteResourceFactory;

	private static final String SMALL_ICON = "small_icon";
	private static final String LOGO = "logo";

	public ServicePage createServicePage(com.doone.telmarket.domain.Service service, Integer recommandGrade,
			String smallIconName, String logoName, String flashName, String serviceSummaryContent) {
		Integer serviceCode = service.getServiceCode();
		String smllIconReplacmentName = concatName(SMALL_ICON, serviceCode.toString());
		SiteResource smallIcon = siteResourceFactory.createSiteResource(smallIconName, smllIconReplacmentName,
				SiteDirectory.IMAGE_DIRECTORY, SiteDirectory.SERVICE_DIRECTORY);
		String logoReplacementName = concatName(LOGO, serviceCode.toString());
		SiteResource logo = siteResourceFactory.createSiteResource(logoName, logoReplacementName,
				SiteDirectory.IMAGE_DIRECTORY, SiteDirectory.SERVICE_DIRECTORY);
		SiteResource flash = siteResourceFactory.createSiteResource(flashName, serviceCode.toString(),
				SiteDirectory.FLASH_DIRECTORY, SiteDirectory.SERVICE_DIRECTORY);
		SiteResource page = siteResourceFactory.createSiteResource(serviceCode.toString(),
				SiteDirectory.PAGE_DIRECTORY, SiteDirectory.SERVICE_DIRECTORY);
		ServicePage servicePage = new ServicePage(service, recommandGrade, smallIcon, logo, flash, page,
				serviceSummaryContent);
		return servicePage;
	}

	public ServicePage createServicePage(com.doone.telmarket.domain.Service service, Integer recommandGrade,
			String smallIconName, String logoName, String flashName, String serviceSummaryContent,
			List<ServicePageContent> contentsMap) {
		ServicePage servicePage = createServicePage(service, recommandGrade, smallIconName, logoName, flashName,
				serviceSummaryContent);
		Iterator<ServicePageContent> it = contentsMap.iterator();
		while (it.hasNext()) {
			ServicePageContent entry = it.next();
			String key = entry.getTitle();
			String value = entry.getContent();
			servicePage.addContent(key.toString(), value.toString());
			System.out.println(key + ":" + value);
		}
		return servicePage;
	}

	public ServicePage createServcePage(com.doone.telmarket.domain.Service service, Integer recommandGrade,
			String smallIconName, String logoName, String flashName, String serviceSummaryContent,
			List<ServicePageContent> contentsMap, String orderMode, String redirectUrl) {
		ServicePage servicePage = createServicePage(service, recommandGrade, smallIconName, logoName, flashName,
				serviceSummaryContent, contentsMap);
		ServiceOrder serviceOrder = new ServiceOrder(orderMode, redirectUrl);
		servicePage.setServiceOrder(serviceOrder);
		return servicePage;
	}

	private String concatName(String beforeName, String afterName) {
		return beforeName.concat("_").concat(afterName);
	}

}
